package main

import (
	"fmt"
	"math/rand"
	"sync"
)

var wg sync.WaitGroup

func getRandNum(out chan<- int) {
	defer wg.Done()
	var random int
	for i := 0; i < 10; i++ {
		random = rand.Intn(30)
		out <- random
	}
	close(out)
}

func addRandNum(in <-chan int, out chan<- int) {
	defer wg.Done()
	for v := range in {
		fmt.Printf("before +1: %d\n", v)
		out <- v + 1
	}
	close(out)
}

func printRes(in <-chan int) {
	defer wg.Done()
	for v := range in {
		fmt.Println("after +1", v)
	}
}

func PipelineMode() {
	wg.Add(3)
	ch1 := make(chan int, 1)
	ch2 := make(chan int, 1)

	go getRandNum(ch1)
	go addRandNum(ch1, ch2)
	go printRes(ch2)

	wg.Wait()
}

func main() {
	PipelineMode()
}
